﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : Lolo
// Created          : 10-27-2013
//
// Last Modified By : Lolo
// Last Modified On : 10-27-2013
// ***********************************************************************
// <copyright file="PortalEnvironment.cs" company="Innovacall">
//     Copyright (c) Innovacall. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************

using InnovaApp.Layers.CommonLayer.Controllers;
using InnovaApp.Layers.CommonLayer.Models.ViewModels.LayoutViewModels;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.DomainLayer;
using INNOVACALL.Framework.Common;
using INNOVACALL.Framework.Common.Models.ViewModels.MetaModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace InnovaApp.Portals.MVC4Portal.Controllers
{
    /// <summary>
    /// Class PortalEnvironment contains environment variable comming from CommonEnvironment of from the database
    /// </summary>
    public static class PortalEnvironment
    {
        #region private members
        private static bool loaded = false;
        private static string _IISApplicationName;
        private static string _JQueryUITheme;
        private static bool _JQueryUIRoundedCorners;
        private static string _SiteBaseUrl;
        private static enSiteLayouts _SiteLayout;
        private static string _KendoUITheme;
        private static string _DefaultLanguage;
        private static string _Languages;
        private static List<Language_MetaModel> _LstLanguages;
        private static string _DynaContentControllersBaseNamespace;
        private static string _CustomControllersBaseNamespace;
        private static string _UploadPath;
        private static string _UploadPhysicalPath;
        private static string _UserEmptyIconPath;
        private static string _UserIconsFolder;
        private static string _UserIconsPhysicalPath;
        private static string _CssBasePath;
        private static int _DefaultAutoCompleateCount;
        private static string _AccountActivationFromMailAddress;
        private static Dictionary<enDynamicSettings, string> _DynamicSettings;
        #endregion

        #region public members
        /// <summary>
        /// Gets the name of the IIS application.
        /// </summary>
        /// <value>The name of the IIS application.</value>
        public static string IISApplicationName
        {
            get
            {
                if (!loaded) Refresh();
                return _IISApplicationName;
            }
        }
        /// <summary>
        /// Gets the name of the SiteLayout.
        /// </summary>
        /// <value>The name of the SiteLayout</value>
        public static enSiteLayouts SiteLayout
        {
            get
            {
                if (!loaded) Refresh();
                return _SiteLayout;
            }
        }
        /// <summary>
        /// Gets the object query unique identifier theme.
        /// </summary>
        /// <value>The object query unique identifier theme.</value>
        public static string JQueryUITheme
        {
            get
            {
                if (!loaded) Refresh();
                return _JQueryUITheme;
            }
        }

        public static bool JQueryUIRoundedCorners
        {
            get
            {
                if (!loaded) Refresh();
                return _JQueryUIRoundedCorners;
            }
        }
        /// <summary>
        /// Gets the site base URL.
        /// </summary>
        /// <value>The site base URL.</value>
        public static string SiteBaseUrl
        {
            get
            {
                if (!loaded) Refresh();
                return _SiteBaseUrl;
            }
        }
        /// <summary>
        /// Gets the kendo unique identifier theme.
        /// </summary>
        /// <value>The kendo unique identifier theme.</value>
        public static string KendoUITheme
        {
            get
            {
                if (!loaded) Refresh();
                return _KendoUITheme;
            }
        }
        /// <summary>
        /// Gets the default language.
        /// </summary>
        /// <value>The default language.</value>
        public static string DefaultLanguage
        {
            get
            {
                if (!loaded) Refresh();
                return _DefaultLanguage;
            }
        }
        /// <summary>
        /// Gets the languages.
        /// </summary>
        /// <value>The languages.</value>
        public static string Languages
        {
            get
            {
                if (!loaded) Refresh();
                return _Languages;
            }
        }
        /// <summary>
        /// Gets the LST languages.
        /// </summary>
        /// <value>The LST languages.</value>
        public static List<Language_MetaModel> LstLanguages
        {
            get
            {
                if (!loaded) Refresh();
                return _LstLanguages;
            }
        }
        /// <summary>
        /// Gets the dyna content controllers base namespace.
        /// </summary>
        /// <value>The dyna content controllers base namespace.</value>
        public static string DynaContentControllersBaseNamespace
        {
            get
            {
                if (!loaded) Refresh();
                return _DynaContentControllersBaseNamespace;
            }
        }
        /// <summary>
        /// Gets the custom controllers base namespace.
        /// </summary>
        /// <value>The custom controllers base namespace.</value>
        public static string CustomControllersBaseNamespace
        {
            get
            {
                if (!loaded) Refresh();
                return _CustomControllersBaseNamespace;
            }
        }
        /// <summary>
        /// Gets the upload path.
        /// </summary>
        /// <value>The upload path.</value>
        public static string UploadPath
        {
            get
            {
                if (!loaded) Refresh();
                return _UploadPath;
            }
        }
        /// <summary>
        /// Gets the upload physical path.
        /// </summary>
        /// <value>The upload physical path.</value>
        public static string UploadPhysicalPath
        {
            get
            {
                if (!loaded) Refresh();
                return _UploadPhysicalPath;
            }
        }
        /// <summary>
        /// Gets the user empty icon path.
        /// </summary>
        /// <value>The user empty icon path.</value>
        public static string UserEmptyIconPath
        {
            get
            {
                if (!loaded) Refresh();
                return _UserEmptyIconPath;
            }
        }
        /// <summary>
        /// Gets the user icons folder.
        /// </summary>
        /// <value>The user icons folder.</value>
        public static string UserIconsFolder
        {
            get
            {
                if (!loaded) Refresh();
                return _UserIconsFolder;
            }
        }
        /// <summary>
        /// Gets the user icons physical path.
        /// </summary>
        /// <value>The user icons physical path.</value>
        public static string UserIconsPhysicalPath
        {
            get
            {
                if (!loaded) Refresh();
                return _UserIconsPhysicalPath;
            }
        }
        /// <summary>
        /// Gets the CSS base path.
        /// </summary>
        /// <value>The CSS base path.</value>
        public static string CssBasePath
        {
            get
            {
                if (!loaded) Refresh();
                return _CssBasePath;
            }
        }
        /// <summary>
        /// Gets the default automatic compleate count.
        /// </summary>
        /// <value>The default automatic compleate count.</value>
        public static int DefaultAutoCompleateCount
        {
            get
            {
                if (!loaded) Refresh();
                return _DefaultAutoCompleateCount;
            }
        }
        /// <summary>
        /// Gets the account activation from mail address.
        /// </summary>
        /// <value>The account activation from mail address.</value>
        public static string AccountActivationFromMailAddress
        {
            get
            {
                if (!loaded) Refresh();
                return _AccountActivationFromMailAddress;
            }
        }
        /// <summary>
        /// The _ dynamic properties
        /// </summary>
        public static Dictionary<enDynamicSettings, string> DynamicSettings
        {
            get
            {
                if (!loaded) Refresh();
                return _DynamicSettings;
            }
        }
        #endregion

        /// <summary>
        /// Refreshes all environment variable
        /// </summary>
        public static void Refresh()
        {
            _IISApplicationName = CommonEnvironment.IISApplicationName;
            _SiteBaseUrl = CommonEnvironment.SiteBaseUrl;
            _DefaultLanguage = CommonEnvironment.DefaultLanguage;
            _Languages = CommonEnvironment.Languages;
            _LstLanguages = CommonEnvironment.LstLanguages;
            _DynaContentControllersBaseNamespace = CommonEnvironment.DynaContentControllersBaseNamespace;
            _UploadPath = CommonEnvironment.UploadPath;
            _UploadPhysicalPath = CommonEnvironment.UploadPhysicalPath;
            _UserEmptyIconPath = CommonEnvironment.UserEmptyIconPath;
            _UserIconsFolder = CommonEnvironment.UserIconsFolder;
            _UserIconsPhysicalPath = CommonEnvironment.UserIconsPhysicalPath;
            _CssBasePath = CommonEnvironment.CssBasePath;
            _DefaultAutoCompleateCount = CommonEnvironment.DefaultAutoCompleateCount;
            _AccountActivationFromMailAddress = CommonEnvironment.AccountActivationFromMailAddress;

            _DynamicSettings = new Dictionary<enDynamicSettings, string>();

            using (DataRepositories _dataContext = new DataRepositories())
            {
                InnoAppSettings dbInnoAppSettings = _dataContext.InnoAppSettings_Rep.GetById(1);
                if (dbInnoAppSettings == null)
                {
                    _JQueryUITheme = CommonEnvironment.JQueryUITheme;
                    _JQueryUIRoundedCorners = CommonEnvironment.JQueryUIRoundedCorners;
                    _KendoUITheme = CommonEnvironment.KendoUITheme;
                    _SiteLayout = CommonEnvironment.SiteLayout;
                }
                else
                {
                    _JQueryUITheme = dbInnoAppSettings.JQueryUITheme;
                    _JQueryUIRoundedCorners = dbInnoAppSettings.JQueryCornersRounded.Value;
                    _KendoUITheme = dbInnoAppSettings.KendoUITheme;
                    _SiteLayout = EnumTools.EnumTypeTo<enSiteLayouts>(dbInnoAppSettings.SiteLayout);
                }
                RefershDynamicSetting(_dataContext);
            }
            loaded = true;
        }
        /// <summary>
        /// Refreshes the specified _data context.
        /// </summary>
        /// <param name="_dataContext">The _data context.</param>
        public static void Refresh(DataRepositories _dataContext)
        {
            _IISApplicationName = CommonEnvironment.IISApplicationName;
            _SiteBaseUrl = CommonEnvironment.SiteBaseUrl;
            _DefaultLanguage = CommonEnvironment.DefaultLanguage;
            _Languages = CommonEnvironment.Languages;
            _LstLanguages = CommonEnvironment.LstLanguages;
            _DynaContentControllersBaseNamespace = CommonEnvironment.DynaContentControllersBaseNamespace;
            _UploadPath = CommonEnvironment.UploadPath;
            _UploadPhysicalPath = CommonEnvironment.UploadPhysicalPath;
            _UserEmptyIconPath = CommonEnvironment.UserEmptyIconPath;
            _UserIconsFolder = CommonEnvironment.UserIconsFolder;
            _UserIconsPhysicalPath = CommonEnvironment.UserIconsPhysicalPath;
            _CssBasePath = CommonEnvironment.CssBasePath;
            _DefaultAutoCompleateCount = CommonEnvironment.DefaultAutoCompleateCount;
            _AccountActivationFromMailAddress = CommonEnvironment.AccountActivationFromMailAddress;

            InnoAppSettings dbInnoAppSettings = _dataContext.InnoAppSettings_Rep.GetById(1);
            if (dbInnoAppSettings == null)
            {
                _JQueryUITheme = CommonEnvironment.JQueryUITheme;
                _JQueryUIRoundedCorners = CommonEnvironment.JQueryUIRoundedCorners;
                _KendoUITheme = CommonEnvironment.KendoUITheme;
                _SiteLayout = CommonEnvironment.SiteLayout;
            }
            else
            {
                _JQueryUITheme = dbInnoAppSettings.JQueryUITheme;
                _JQueryUIRoundedCorners = dbInnoAppSettings.JQueryCornersRounded.Value;
                _KendoUITheme = dbInnoAppSettings.KendoUITheme;
                _SiteLayout = EnumTools.EnumTypeTo<enSiteLayouts>(dbInnoAppSettings.SiteLayout);
            }
            RefershDynamicSetting(_dataContext);
            loaded = true;
        }
        /// <summary>
        /// Refershes the dynamic setting.
        /// </summary>
        /// <param name="_dataContext">The _data context.</param>
        private static void RefershDynamicSetting(DataRepositories _dataContext)
        {
            IEnumerable<InnoAppDynamicSetting> dbInnoAppDynamicSettings = _dataContext.InnoAppDynamicSetting_Rep.GetAll().AsEnumerable();
            _DynamicSettings = new Dictionary<enDynamicSettings, string>();
            foreach (InnoAppDynamicSetting dbInnoAppDynamicSetting in dbInnoAppDynamicSettings)
            {
                _DynamicSettings.Add(EnumTools.EnumTypeTo<enDynamicSettings>(dbInnoAppDynamicSetting.Id), dbInnoAppDynamicSetting.Value);
            }
        }
    }
}